//
//  SendSuggestionViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/24.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//********意见反馈********
#import "SendSuggestionViewController.h"

@interface SendSuggestionViewController ()<UITextViewDelegate>

@property(nonatomic,copy)UITextView *contentTextView;

@property(nonatomic,copy)UITextField *contactTextField;
@end

@implementation SendSuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    [self createUI];
    // Do any additional setup after loading the view.
}

-(void)createUI
{
    UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(5, 20+44+5, self.view.frame.size.width-5-5, 20)];
    lab1.text=@"反馈内容";
    lab1.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    lab1.font=[UIFont systemFontOfSize:16];
    [self.view addSubview:lab1];
    
    
    UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(lab1.frame)+5, self.view.frame.size.width-5-5, 100)];
    //textView.backgroundColor=[UIColor grayColor];
    textView.text=@"请填写反馈内容(最少5个字)";
    textView.delegate=self;
    textView.textColor=[UIColor grayColor];
    textView.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    textView.layer.borderWidth=1.0;
    textView.layer.cornerRadius=5.0;
    textView.font=[UIFont systemFontOfSize:15];
    [self.view addSubview:textView];
    _contentTextView=textView;
    
    UILabel *lab2=[[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(textView.frame)+5, self.view.frame.size.width-5-5, 20)];
    lab2.text=@"联系方式";
    lab2.textColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    lab2.font=[UIFont systemFontOfSize:16];
    [self.view addSubview:lab2];
    
    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(lab2.frame)+5, self.view.frame.size.width-5-5, 30)];
    textField.placeholder=@" 电话/邮箱/qq";
    textField.textColor=[UIColor grayColor];
    textField.font=[UIFont systemFontOfSize:15];
    textField.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    textField.layer.borderWidth=1.0;
    textField.layer.cornerRadius=5.0;
    //textField.textAlignment=NSTextAlignmentCenter;
    //textField.backgroundColor=[UIColor grayColor];
    [self.view addSubview:textField];
    _contactTextField=textField;
    
    //确认
    UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    changeBtn.frame=CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMaxY(textField.frame)+15, 150, 35);
    [changeBtn setTitle:@"确认" forState:UIControlStateNormal];
    [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changeBtn.titleLabel.font=[UIFont systemFontOfSize:20];
    changeBtn.backgroundColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    changeBtn.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    changeBtn.layer.borderWidth=2.0;
    changeBtn.layer.cornerRadius=5.0;
    [changeBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBtn];
    
}
//确认
-(void)changeBtnClick:(UIButton *)sender
{
    [self sendSuggest];
}
-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"意见反馈";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=NO;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    textView.text=nil;
    textView.textColor=[UIColor blackColor];
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"请填写反馈内容(最少5个字)"]) {
        
    }
    if (textView.text.length<1) {
        textView.text=@"请填写反馈内容(最少5个字)";
    }
    textView.textColor=[UIColor colorWithRed:181/255.0 green:181/255.0 blue:187/255.0 alpha:1];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.navigationController.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//用户反馈
-(void)sendSuggest
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSDictionary *params=@{@"content":_contentTextView.text,@"contact":_contactTextField.text};
    
    if ([_contentTextView.text isEqualToString:@"请填写反馈内容(最少5个字)" ]||_contentTextView.text.length < 5) {
        [self.view showTiShiMessage:@"请最少输入5个字"];
    }else if (_contactTextField.text.length==0){
        [self.view showTiShiMessage:@"请填写你的联系方式"];
    }else{
        [manager POST:SendSuggest parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [self.view showTiShiMessage:@"反馈成功"];
            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"用户反馈%@",dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error%@",error.localizedDescription);
        }];
    }
}


//-(void)viewWillAppear:(BOOL)animated
//{
//    [self.navigationController.navigationBar setTranslucent:YES];
//}
//
//-(void)viewWillDisappear:(BOOL)animated
//{
//    [self.navigationController.navigationBar setTranslucent:NO];
//}


@end
